package com.badshun.util;

import com.badlogic.gdx.files.*;
import java.io.*;
import java.util.*;

public class BadDataLibrary {
	private FileHandle fileHandle;
	private BufferedReader reader;
	
	private int index = 0;
	
	public BadDataLibrary(FileHandle fileHandle) {
		this.fileHandle = fileHandle;
	}
	
	public HashMap<Integer,HashMap<String,String>> getData() {
		try {
			int keyIndex = 0;
			
			reader = new BufferedReader(new InputStreamReader(fileHandle.read()));

			HashMap<Integer,HashMap<String,String>> totalMap = new HashMap<Integer,HashMap<String,String>>();

			HashMap<String,String> dataMap;

			String content = "";

			ArrayList<String> contentList = new ArrayList<String>();

			while((content = reader.readLine()) != null) {
				contentList.add(content.trim());
			}


			if(contentList.get(0).equals("[?>BadDataLibrary<?]")) {
				if(getNext(contentList).equals("[NPCPlayerData]")) {
					while(true) {
						String c;

						if((c = getNext(contentList)).equals("[NPCPlayer]")) {
							dataMap = new HashMap<String,String>();

							c = getNext(contentList);
							if(c.startsWith("[Name]")) {
								dataMap.put("Name",c.replace("[Name]","").replace("[/Name]",""));

								c = getNext(contentList);
								if(c.startsWith("[Head]")) {
									dataMap.put("Head",c.replace("[Head]","").replace("[/Head]",""));

									c = getNext(contentList);
									if(c.startsWith("[Action]")) {
										dataMap.put("Action",c.replace("[Action]","").replace("[/Action]",""));

										c = getNext(contentList);
										if(c.startsWith("[Direction]")) {
											dataMap.put("Direction",c.replace("[Direction]","").replace("[/Direction]",""));

											c = getNext(contentList);
											if(c.startsWith("[Player]")) {
												dataMap.put("Player",c.replace("[Player]","").replace("[/Player]",""));

												c = getNext(contentList);
												if(c.startsWith("[Position]")) {
													dataMap.put("Position",c.replace("[Position]","").replace("[/Position]",""));

													c = getNext(contentList);
													if(c.startsWith("[Talk]")) {
														dataMap.put("Talk",c.replace("[Talk]","").replace("[/Talk]",""));

														c = getNext(contentList);
														if(c.startsWith("[/NPCPlayer]")) {
															totalMap.put(keyIndex,dataMap);
															
															keyIndex++;
															
															dataMap = null;
															
															continue;

														} else {
															break;
														}
													} else {
														break;
													}
												} else {
													break;
												}
											} else {
												break;
											}
										} else {
											break;
										}
									} else {
										break;
									}
								} else {
									break;
								}
							} else {
								break;
							}
						} else {
							break;
						}
					}
				}
				
				return totalMap;
			}

			return null;

		} catch(IOException ioe) {
			ioe.printStackTrace();

			return null;
		}
	}
	
	private String getNext(ArrayList<String> al) {
		index++;
		
		if(index < al.size()) {
			return al.get(index);
		} else {
			return "";
		}
	}
}
